 /**
 * Description: 物资类别
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.sys.mapper.ISysMaterialsTypeMapper;
import com.metro.paperless.sys.service.inf.ISysMaterialsTypeService;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.sys.beans.vo.SysMaterialsType;
import com.metro.paperless.sys.beans.vo.SysMaterialsUnit;

 /**
 * 物资类别<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("sysmaterialstypeService")
public class SysMaterialsTypeService extends BaseServiceSupport<SysMaterialsType> implements ISysMaterialsTypeService{
    //物资类别Mapper
    @Autowired
    private ISysMaterialsTypeMapper sysmaterialstypeMapper;

    @Override
    public IMapper getMapper() {
        return sysmaterialstypeMapper;
    }

    @Override
    public IResult save(SysMaterialsType entity) {
        try {
            sysmaterialstypeMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资类别失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysMaterialsType entity) {
        try {
            sysmaterialstypeMapper.updateNotEmpty(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资类别失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysmaterialstypeMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysmaterialstypeMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
    
    /**
     * 判断类别是否已存在
     */
    public IResult checkMaterialsType(SysMaterialsType entity) {
    	//判断是否已存在
    	List<SysMaterialsType> list = sysmaterialstypeMapper.queryForList();
    	if(list != null && list.size()>0 ) {
    		for(int i=0;i<list.size();i++){
        		if(list.get(i).getTypeName().equals(entity.getTypeName()) && list.get(i).getDataStatus() == 1 ) {
        			return ResultCode.DUPLICATE_KEY;
        		}
        	}
    	}
    	return ResultCode.SUCCESS;
    }
    
    @Override
    public IResult saveMaterialsType(SysMaterialsType entity) {
    	//编号自增
    	SysMaterialsType entity1 = sysmaterialstypeMapper.queryTopList();
    	if(entity1 != null) {
    		int num = Integer.parseInt(entity1.getTypeCode());
    		num = num +1;
    		entity.setTypeCode(String.valueOf(num));
    	}else {
    		entity.setTypeCode("1");
    	}
    	
        try {
            sysmaterialstypeMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资类别失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }
}